package com.example.smartcity.ui.act

import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity.R
import com.example.smartcity.adapter.SellerListAdapter
import com.example.smartcity.app.App
import com.example.smartcity.http.SellerBean
import com.example.smartcity.http.SellerListResp
import com.example.smartcity.utils.CommonUtil
import com.example.smartcity.utils.KeyboardUtil
import kotlinx.android.synthetic.main.activity_tof_seller_list.*
import kotlinx.android.synthetic.main.layout_app_bar_common.*

class TOFSellerListActivity : AppCompatActivity() {

    private var sellers: MutableList<SellerBean> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tof_seller_list)
        tvAppBarTitle.text = "店铺列表"
        ivAppBarLeft.setOnClickListener { finish() }
        edtSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                var keyword = edtSearch.text.toString()
                if (keyword.isBlank()) {
                    CommonUtil.showToast(this, edtSearch.hint.toString())
                } else {
                    doSearch(keyword)
                }
                KeyboardUtil.hideKeyboard(edtSearch.windowToken, this)
                true
            }
            false
        }
        val linearLayoutManager = LinearLayoutManager(this@TOFSellerListActivity)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        rvSellerList.layoutManager = linearLayoutManager
        rvSellerList.adapter = SellerListAdapter(sellers,
            object : SellerListAdapter.OnItemClickListener {
                override fun onClick(position: Int) {
                    val intent =
                        Intent(this@TOFSellerListActivity, TOFSellerDetailActivity::class.java)
                    intent.putExtra("seller_id", sellers[position].id)
                    startActivity(intent)
                }
            })
        loadSellerData()
    }

    // 执行搜索
    private fun doSearch(keyword: String) {
        searchAsSellerName(keyword)
        searchAsProductName(keyword)
    }

    // 作为菜品名搜索
    private fun searchAsProductName(keyword: String) {
        App.fetchData<SellerListResp>(
            "http://124.93.196.45:10001/prod-api/api/takeout/search?keyword=$keyword",
            SellerListResp::class.java
        ) {
            sellers.clear();
            sellers.addAll(it.rows)
            rvSellerList.adapter?.notifyDataSetChanged()
        }
    }

    // 作为店名搜索
    private fun searchAsSellerName(keyword: String) {
        App.fetchData<SellerListResp>(
            "http://124.93.196.45:10001/prod-api/api/takeout/seller/list?name=$keyword",
            SellerListResp::class.java
        ) {
            sellers.clear();
            sellers.addAll(it.rows)
            rvSellerList.adapter?.notifyDataSetChanged()
        }
    }

    // 根据分类读取店铺数据
    private fun loadSellerData() {
        var themeId = intent.extras?.get("theme_id")
        App.fetchData<SellerListResp>(
            "http://124.93.196.45:10001/prod-api/api/takeout/seller/list?themeId=$themeId",
            SellerListResp::class.java
        ) {
            sellers.clear();
            sellers.addAll(it.rows)
            rvSellerList.adapter?.notifyDataSetChanged()
        }
    }

}